/*
 * Session.java
 *
 * Created on March 11, 2007, 2:44 PM
 *
 *
 */

package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.*;
import javax.swing.JOptionPane;

/**Contains current session information
 *
 * @author Travis Kulbeth
 */
public class Session {
    
    private String sessionID = null;
    private EmployeeBO currentEmp = null;
    private StoreBO currentStore = null;
    
    /** Creates a new instance of Session */
    public Session() {
    }
    
    /** Creates a new instance of Session with an id
     *@param id contains the id for this session
     */
    public Session(String id) {
        this.setSessionID(id);
    }
    
    /**
     * Creates a session object for use during the current login session
     *
     * @param storeID contains the storeID for this session
     * @param empID contains the employeeID for this session
     * @param pwd contains the password that the employee entered
     * @throws java.lang.Exception for any errors encountered in creating the Session
     * @return Session object
     */
    public static synchronized Session create(EmployeeBO emp, StoreBO store) throws Exception{
        Session curOps = new Session(GUID.generate());              //create a session object with an id
        curOps.setStore(store);      //retrieve and set the Store
        curOps.updateSession(emp);                           //get the Employee
        Cache c = Cache.getInstance();
        c.put(curOps.getSessionID(), curOps);
        return curOps;
    }
    
    /** Returns the Employee for this session
     *@return currentEmp
     */
    public EmployeeBO getEmployee(){
        return this.currentEmp;
    }
    
    /** Sets the Employee for this session
     *@param emp contains the employee for this session
     */
    public void setEmployee(EmployeeBO emp){
        this.currentEmp = emp;
    }
    
    /** Returns the Store for this session
     *@return currentStore
     */
    public StoreBO getStore(){
        return this.currentStore;
    }
    
    /** Sets the Store for this session
     *@param store contains the store information for this session
     */
    public void setStore(StoreBO store){
        this.currentStore = store;
    }
    
    /** Returns the sessionID
     *@return sessionID
     */
    public String getSessionID() {
        return sessionID;
    }
    
    /** Sets the sessionID
     *@param sessionID contains the ID for this session
     */
    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }
    
    /** Updates the components of the current session
     *@param empID contains the employeeID for this session
     *@param pwd contains the password that the employee entered
     * @throws java.lang.Exception for any errors encountered in updating the Session
     */
    public void updateSession(EmployeeBO emp) throws Exception {
        this.setEmployee(emp);
        Cache c = Cache.getInstance();
        c.put(this.getSessionID(), this);
    }
}
